import React from "react";
import {
    createSlice
} from "@reduxjs/toolkit";
import {useSelector} from "react-redux";
import {
    delBatchRemove,
    delHospitalSetById, getHospitalList, getHospitalListById,
    getHospitalSet,
    getHospitalSetLockId,
    postHospitalSet,
    putHospitalSet, updateHospitalListById
} from "@/api/hosp";
import {changeLoading, changePageInfo, changeTitleByKey} from "@/store/slice/common";
const hospSlice = createSlice({
    name: 'hosp',
    initialState: {
        hospSetList:[],
        hospListList: [],
        hospListListInfo: {
            hospital: {
                param: {}
            },
            bookingRule:{
                rule: []
            }
        },//根据ID获取的医院列表，单个医院的详情
        params: {},//增加一个初始状态，记录query参数，传参是要写成params: {hosname, hoscode}
    },
    reducers: {
        upHospSetList(state,{payload}){
            state.hospSetList = payload;
        },
        changeStateById(state, {payload}) {
            const info = state.hospSetList.find((v:any) => v.id === payload) as any;
            console.log(info.status, '----info')
            info.status = info.status === 0 ? 1 : 0;
            console.log(info.status, '----info111')
        },
        // query参数状态  //优化搜索
        changeQuery(state, {payload}) {
            state.params = payload;
        },
        //医院列表
        upHospListList(state, {payload}) {
            state.hospListList = payload;
        },
        //医院详情
        upHospListListInfo(state, {payload}) {
            state.hospListListInfo = payload;
        }

    }
})


export const {upHospSetList, changeStateById, upHospListList, changeQuery, upHospListListInfo} = hospSlice.actions;
//获取列表
export const getHospitalSetAsync = (page: number, limit: number) => {
    return async (dispatch:any, getState: any) => {
        dispatch(changeLoading(true))
        const result = await getHospitalSet(page, limit, getState().hosp.params); // 优化搜索
        // console.log(result.data)
        dispatch(upHospSetList(result.data.records));
        dispatch(changePageInfo({
            current:result.data.current,
            total:result.data.total,
            pageSize:result.data.size
        }))
         dispatch(changeLoading(false))
    }
}
//锁定1
//不能实时更新，但是每次都是在自己的界面里操作，如每次加一，给用户体验度更真实，看需求选择锁定的方式
// export const getHospitalSetLockIdAsync = (id: number, status: number) => {
//     return async(dispatch:any) => {
//         status = status === 0 ? 1 : 0;
//         const res = await getHospitalSetLockId(id, status);
//         console.log(res,'=====')
//         dispatch(changeStateById(id))
//     }
// }

//锁定2
// 完成锁定2:更新服务端的锁定状态，然后重新获取当前页的信息，进行渲染
// 优点：可以将最后操作以后的最新数据进行呈现
export const getHospitalSetLockIdAsync = (id: number, status: number) => {
    return async(dispatch:any, getState:any) => {
        const res = await getHospitalSetLockId(id, status);
        console.log(res,'=====')
        // dispatch(getHospitalSetAsync(1, 5))
        const {current,pageSize} = getState().common.pageInfo
        dispatch(getHospitalSetAsync(current,pageSize));
    }
}
//postHospitalSetAsync 添加医院设置
export const postHospitalSetAsync = (body:any) => {
    return async (dispatch:any, getState:any) => {
        const {current, pageSize} = getState().common.pageInfo;
        if (body.id) {
            await putHospitalSet(body);//修改
            await dispatch(getHospitalSetAsync(current,pageSize));
        }else {
            await postHospitalSet(body);//添加
            await dispatch(getHospitalSetAsync(1,pageSize));
        }


    }
}
//根据ID删除医院设置
export const delHospitalSetByIdAsync = (id: number) => {
    return async (dispatch: any, getState: any) => {
        await delHospitalSetById(id);
        const {current, pageSize} = getState().common.pageInfo;
        await dispatch(getHospitalSetAsync(current,pageSize));
    }
}
//批量删除医院设置
export const delBatchRemoveAsync = (data: React.Key[]) => {
    return async (dispatch: any, getState: any) => {
        await delBatchRemove(data);
        const {current, pageSize} = getState().common.pageInfo;
        await dispatch(getHospitalSetAsync(current,pageSize));
    }
}

//批量获取医院列表
export const getHospitalListAsync = (page:number, limit: number) => {
    return async(dispatch: any,  getState: any) => {
        const result = await getHospitalList(page, limit, getState().hosp.params);
        // console.log(result)
        //此时把当前页码存起来放在localstorage中
        localStorage.setItem("current",result.data.number+1);

        await dispatch(upHospListList(result.data.content));
        await dispatch(changePageInfo({
            current: result.data.number + 1,
            pageSize: result.data.size,
            total: result.data.totalElements
        }))
    }
}
//获取医院详情ById
export const getHospitalListByIdAsync = (id: string) => {
    return async(dispatch:any) => {
        const result = await getHospitalListById(id);
        dispatch(upHospListListInfo(result.data));
        return result;
    }
}
//改变医院列表状态
export const updateHospitalListByIdAsync = (id: string, status: number) => {
    return async(dispatch:any, getState: any) => {
        await updateHospitalListById(id, status);
        const {current, pageSize} = getState().common.pageInfo;
        dispatch(getHospitalListAsync(current, pageSize))

    }
}
export const useSelectorHosp = () => useSelector((state:any) => state.hosp);
export default hospSlice.reducer;